package com.example.understand_compose1.fet_xuiusage.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.FragmentHomeBinding

// TODO: 默认使用Bottom Navigation Activity 创建的类
class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.textHome
        homeViewModel.text.observe(viewLifecycleOwner) {
            textView.text = it
        }
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

// TODO: 构造函数方式关联home fragment 资源文件
//class HomeFragment : Fragment(R.layout.fragment_home) {
//    val binding: FragmentHomeBinding = FragmentHomeBinding.inflate(layoutInflater)
//
//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        super.onViewCreated(view, savedInstanceState)
//        binding.recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
////        binding.recyclerView.adapter =
//    }
//
//
//    inner class HomeAdapter: RecyclerView.Adapter<HomeViewHolder>() {
//        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
//            // TODO:切记，第三个参数应该为false
//            val item_view = LayoutInflater.from(context).inflate(R.layout.item_view_linear_vertical,parent,false)
//            return HomeViewHolder(item_view)
//        }
//
//        override fun getItemCount(): Int {
//            return 20
//        }
//
//        override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
//            holder.itemView.findViewById<ImageView>(R.id.item_image)
//                .setImageResource(R.drawable.ic_send)
//            holder.itemView.findViewById<TextView>(R.id.item_title).text = "Item $position: 哈哈哈"
//            holder.itemView.findViewById<TextView>(R.id.item_content).text = "这是一个示例文本"
//        }
//
//    }
//
//    class HomeViewHolder(view: View) : RecyclerView.ViewHolder(view) {}
//}